#!/bin/sh

if [ $# = 0 ] ; then
	cat <<EOF
Usage: find-script-command command [grep-options...]

Finds a script command in arx script files.

For optimum performance cd to graph/obj3d/interactive and then run e.g.
 find-script-command teleport

This script will automatically search for all different encodings of the
command, ignoring case and adding as many underscores as needed.
The provided command should *not* already contain any underscores.

It will however not actually parse scripts so false positive matches
(for example in strings and comments) are very much possible.
EOF
	exit 1
fi

nosym='![a-zA-Z_0-9]'
pattern="(?<$nosym)$(printf '%s' "$1" | sed 's/./\_\*\0/g')_*(?$nosym)"
shift

# Some grep/pcre versions can't deal with invalid UTF-8 sequences!
# The script files use one of the ISO-8859-* encodings, but as we only care about
# ASCII for script command names, use the "C" locale - others aren't guaranteed to
# be available.
export LC_ALL=C

grep --color --ignore-case --perl-regexp --recursive --text --include="*.asl*" \
	"$pattern" "$@"
